﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace UcAsp.Net.BLL
{
    public class ThemeConfig
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        public static string ThemeName { get; set; }
        public static UcAsp.Net.Model.Theme Theme
        {
            get
            {
                UcAsp.Net.Model.Theme model = new UcAsp.Net.Model.Theme();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\themes\\" + ThemeName + "\\config\\default.xml");
                string xmlpath = ("ManageTheme");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {
                        foreach (XmlNode xn in xns)
                        {
                            for (int i = 0; i < xn.ChildNodes.Count; i++)
                            {
                                XmlToObject(model, xn.Name, xn.InnerText);
                            }
                        }
                    }
                }

                return model;
            }
        }
        protected static void XmlToObject(object oPut, string name, string value)
        {
            System.ComponentModel.PropertyDescriptor pd = System.ComponentModel.TypeDescriptor.GetProperties(oPut).Find(name, true);
            if (pd != null)
            {
                if (pd.PropertyType == typeof(int) || pd.PropertyType == typeof(long) || pd.PropertyType == typeof(string) || pd.PropertyType == typeof(DateTime) || pd.PropertyType == typeof(bool))
                {
                    if (pd.PropertyType == typeof(int))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt32(value));
                    }
                    else if (pd.PropertyType == typeof(long))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt64(value));
                    }
                    else if (pd.PropertyType == typeof(double))
                    {
                        pd.SetValue(oPut, System.Convert.ToDouble(value));
                    }
                    else if (pd.PropertyType == typeof(bool))
                    {
                        pd.SetValue(oPut, System.Convert.ToBoolean(value));
                    }
                    else
                    {
                        if (value == null)
                            pd.SetValue(oPut, string.Empty);
                        else
                            pd.SetValue(oPut, value);
                    }
                }
            }
            else
            {
                //对象为空
            }
        }
        public List<Model.Theme> GetList()
        {
            string[] dir = Directory.GetDirectories(path + "themes");
            List<Model.Theme> list = new List<Model.Theme>();
            for (int i = 0; i < dir.Length; i++)
            {
                string[] _temp = dir[i].Split('\\');
                ThemeConfig.ThemeName = _temp[_temp.Length - 1];
                Model.Theme thems = ThemeConfig.Theme;
                list.Add(thems);
            }
            return list;
        }

        public string[] CategoriesTheme()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "themes\\" + BLL.SettingConfig.GetSiteSetting.Theme + "\\Categories";
            DirectoryInfo info = new DirectoryInfo(path);
            FileInfo[] file = info.GetFiles();
            string[] dir = new string[file.Length];
            for (int i = 0; i < file.Length; i++)
            {
                dir[i] = file[i].Name.Replace(file[i].Extension, "");
            }
            return dir;
        }
        public string[] ShowMessageTheme()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "themes\\" + BLL.SettingConfig.GetSiteSetting.Theme + "\\ShowMessage";
            DirectoryInfo info = new DirectoryInfo(path);
            FileInfo[] file = info.GetFiles();
            string[] dir = new string[file.Length + 1];
            for (int i = 0; i < file.Length; i++)
            {
                dir[i] = file[i].Name.Replace(file[i].Extension, "");
            }
            dir[file.Length] = "/";
            return dir;
        }
    }
}